package dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import util.Conversion;

import dto.EmployeeDTO;
import entities.Employee;

/**
 * Session Bean implementation class EmployeeDAO
 */
@Stateless
public class EmployeeDAO implements EmployeeDAORemote {

	@PersistenceContext
	EntityManager em;
	
	Conversion conv;

	/**
	 * Default constructor.
	 */
	public EmployeeDAO() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public EmployeeDTO findEmployeeByUsernameAndPassword(EmployeeDTO employeeDTO) {
		System.out.println("Nemcsics: " + employeeDTO.getUsername());
		TypedQuery<Employee> query = em
				.createQuery(
						"SELECT e FROM Employee e WHERE e.username = :usernameParam and e.password=:userpass",
						Employee.class);
		query.setParameter("usernameParam", employeeDTO.getUsername());
		query.setParameter("userpass", employeeDTO.getPassword());
		List<Employee> employee = query.getResultList();
		EmployeeDTO result = new EmployeeDTO();
		if (employee != null && employee.size() > 0) {
			
			try {
				result.setEmail(employee.get(0).getEmail());
				result.setPassword(employee.get(0).getPassword());
				result.setUsername(employee.get(0).getUsername());
				result.setId(employee.get(0).getId());
				result.setRank(employee.get(0).getRank());
				result.setName(employee.get(0).getFirstname());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				System.out.println("Bejon ide...");
				e1.printStackTrace();
			}
			

		}
		else
			System.out.println("KAKA!!!!");
		return result;
	}

	@Override
	public EmployeeDTO getEmployeeById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	

	

	


	




	
}
